window.onload = refresh;
window.onload = init;

var code1=document.getElementById("code").value;
var b=document.getElementById("buttons");
var mv=document.getElementById("mainview");
var v=document.getElementById("view");
var mc=document.getElementById("maincode");
var c=document.getElementById("code");
var cm=document.body.getElementsByClassName("CodeMirror")[0];
var call1=document.getElementById("call1");
var end1=document.getElementById("end1");
var centerb=document.getElementById("center");
var call2=document.getElementById("call2");
var down=document.getElementById("up");
var down=document.getElementById("down");
var down=document.getElementById("left");
var down=document.getElementById("right"); 
var end2=document.getElementById("end2");
var tcm=document.getElementById("tcm");
var tar=document.getElementById("tar");
var ra=document.getElementById("ra");
var cell1=document.getElementById("cell1");
var cell2=document.getElementById("cell2");
var showhidediv=document.getElementById("showhidediv");
var wth1=640;
var wth2=1288;


function init() 
{
tcm.value='EDITOR : ADVANCED';
if (tcm.value=='EDITOR : ADVANCED') 
	{
codeMirror = CodeMirror.fromTextArea(document.getElementById("code"), {lineNumbers: true, matchBrackets: true, lineWrapping: true, mode: "text/html", tabMode: "indent", onCursorActivity: function() {codeMirror.setLineClass(hlLine, null);hlLine = codeMirror.setLineClass(codeMirror.getCursor().line, "activeline");}});
var hlLine = codeMirror.setLineClass(0, "activeline");	
        }
refresh();
c.oninput=refresh;
document.body.getElementsByClassName("CodeMirror")[0].onkeyup=refresh;
mirrorerror();
}


function refresh() 
{
call1.value="Again";
v.src="";
 
if (tcm.value=='EDITOR : ADVANCED') 
	{
	view.document.open();
	view.document.write(codeMirror.getValue());
	view.document.close();
	}
  else{
	view.document.open();
	view.document.write(c.value);
	view.document.close();
	}

call1.value=">>OK<<";
setTimeout('call1.value="Refresh"',1250);
}



function mirrorerror()
{
if(end2.value=='Hide-View')
{
document.body.getElementsByClassName("CodeMirror")[0].style.width =wth1 + "px";
}
else
{
document.body.getElementsByClassName("CodeMirror")[0].style.width =wth2 + "px";
}
}

function togglecodemirror() 
{
 if (tcm.value=='EDITOR : ADVANCED') 
	{
	document.body.getElementsByClassName("CodeMirror")[0].style.display = "none";
	document.getElementById("code").style.display = "block";
	document.getElementById("code").value = codeMirror.getValue();
	tcm.value='EDITOR : BASIC ';
	tcm.id='acttcm';
	}
  else  {
	document.body.getElementsByClassName("CodeMirror")[0].style.display = "block";
	document.getElementById("code").style.display = "none";
	codeMirror.setValue(document.getElementById("code").value);
	tcm.value='EDITOR : ADVANCED';
	tcm.id='tcm';
	}
}


function clearall() 
{
  if (tcm.value=='EDITOR : ADVANCED') 
	{
	codeMirror.setValue("");
	}
  else  {
	c.value="";
	}

end1.value=">>OK<<";
setTimeout('end1.value="Clear"',1250);
refresh();
}




function togglecode() 
{
if(call2.value=='Hide-Code')
{
cell1.style.display="none";
call2.id='actce';
call2.value='Show-Code';
cell2.style.display="";
end2.id='end2';
end2.value='Hide-View';mirrorerror();
}
else
{
cell1.style.display="";
call2.id='call2';
call2.value='Hide-Code';mirrorerror();
}}



function toggleview() 
{
if(end2.value=='Hide-View')
{
cell2.style.display="none";
end2.id='actce';
end2.value='Show-View';
cell1.style.display="";
call2.id='call2';
call2.value='Hide-Code';mirrorerror();
}
else
{
cell2.style.display="";
end2.id='end2';
end2.value='Hide-View';mirrorerror();
}
}





function trefresh()
{
if(tar.value=='AUTO-REFRESH : OFF')
{
c.oninput=refresh;
document.body.getElementsByClassName("CodeMirror")[0].onkeyup=refresh;
tar.value='AUTO-REFRESH : ON';
tar.id='tar';
}
else
{
c.oninput=null;
document.body.getElementsByClassName("CodeMirror")[0].onkeyup=null;
tar.value='AUTO-REFRESH : OFF'
tar.id='acttar';
}
}



function resetall()
{
if (tcm.value=='EDITOR : ADVANCED') 
	{
	codeMirror.setValue(code1);
	}
  else  {
	c.value=code1;
	}
refresh();
c.oninput=refresh;
document.body.getElementsByClassName("CodeMirror")[0].onkeyup=refresh;
if(tar.value=="AUTO-REFRESH : OFF")
trefresh();
if(tcm.value=="EDITOR : BASIC ")
togglecodemirror()
if(call2.value=="Show-Code")
togglecode();
if(end2.value=="Show-View")
toggleview();
ra.value=">>OK<<"
setTimeout('ra.value="RESET"',1250);
}


function dropdown()
{
 if(showhidediv.innerHTML=="^")
  {
   showhidediv.innerHTML="x";
   showhidediv.title="Hide Controls";
   $(document).ready(function(){
$("#buttons").slideDown(500);
});
  }
  else
  {
   showhidediv.innerHTML="^";
   showhidediv.title="Show Controls";
   $(document).ready(function(){
$("#buttons").slideUp(500);
});
  }
}